package poo.testo;

import java.util.*;
import java.io.*;

public class GestoreTesto {

	public enum Simbolo {
		WORD, EndOfFile;
	}

	private boolean EOF = false;

	private String linea = null;

	private Scanner input, scan;

	private int nLC = 0;

	private String word;

	public GestoreTesto(File f) throws IOException {
		input = new Scanner(f);
	}

	public Simbolo prossimoSimbolo() throws IOException {
		do {
			avanza();
		} while (!EOF && !scan.hasNext());
		if (EOF)
			return Simbolo.EndOfFile;
		word = scan.next();
		return Simbolo.WORD;
	}

	public String getString() {
		return word;
	}
	

	public int getNumeroLinea() {
		return nLC;
	}

	private void avanza() {
		try {
			if (linea == null || !scan.hasNext()) {
				linea = input.nextLine();
				nLC++;
				System.out.println(nLC + ": " + linea);
				scan = new Scanner(linea);
				scan.useDelimiter("[^a-zA-Z_]+");
			}
		} catch (Exception e) {
			EOF = true;
			input.close();
		}
	}

	public static void main(String[] args) throws IOException {
		GestoreTesto gt;
		Scanner sc = new Scanner(System.in);
		System.out.println("Fornire file testo: ");
		String nomeFile = sc.nextLine();
		File f = new File(nomeFile);
		gt = new GestoreTesto(f);

		GestoreTesto.Simbolo sim = gt.prossimoSimbolo();
		while (sim != GestoreTesto.Simbolo.EndOfFile) {
			String parCor = gt.getString();
			System.out.println(parCor);
			sim = gt.prossimoSimbolo();
		}
		sc.close();
	}
}
